IP = "192.168.1.77"
PORT = 2222
MODE = 1
SELECTED = 1
TOPSCREEN = 0
MAXSCREEN = 21
CONNECTIONCONFIG = 1
currenttable = {}

red = Color.new(255, 0, 0)
black = Color.new(0, 0, 0)
white = Color.new(255, 255, 255)
buffer = Image.createEmpty(480, 272)
background = Image.load("background.jpg")
if System.setLow ~= nil then System.setLow() end

Wlan.init()
wlanconfigs = Wlan.getConnectionConfigs()
for k,v in pairs(wlanconfigs) do
	if k == 1 then
		apn = v
	end
end
Wlan.useConnectionConfig(CONNECTIONCONFIG)
PSPIP = Wlan.getIPAddress()
nmpd = Socket.connect(IP, PORT)

function table.load( sfile )
   if string.sub( sfile,-3,-1 ) == "--|" then
      tables,err = loadstring( sfile )
   else
      tables,err = loadfile( sfile )
   end
   if err then return _,err
   end
   tables = tables()
   for idx = 1,#tables do
      local tolinkv,tolinki = {},{}
      for i,v in pairs( tables[idx] ) do
         if type( v ) == "table" and tables[v[1]] then
            table.insert( tolinkv,{ i,tables[v[1]] } )
         end
         if type( i ) == "table" and tables[i[1]] then
            table.insert( tolinki,{ i,tables[i[1]] } )
         end
      end
      for _,v in ipairs( tolinkv ) do
         tables[idx][v[1]] = v[2]
      end
      for _,v in ipairs( tolinki ) do
         tables[idx][v[2]],tables[idx][v[1]] =  tables[idx][v[1]],nil
      end
   end
   return tables[1]
end 

LIBRARY, ERROR = table.load("library.nmpd")

function printListing(ltype, param)
	if ltype == 1 then
		y = 20
		artists = 0
		currenttable = {}
		for k,v in pairs(LIBRARY) do
			table.insert(currenttable, k)
		end
		table.sort(currenttable, function(a, b) return a:lower() < b:lower() end)
		for k,v in pairs(currenttable) do
			if k <= TOPSCREEN then
			else
				if y < 230 then
					if k == SELECTED then buffer:print(5, y, v, red) else buffer:print(5, y, v, white) end
					y = y + 10
				end
			end
			artists = artists + 1
		end
		MAXLIMIT = artists
	elseif ltype == 2 then
		y = 20
		albums = 0
		currenttable = {}
		for k,v in pairs(LIBRARY[param]) do
			table.insert(currenttable, k)
		end
		table.sort(currenttable, function(a, b) return a:lower() < b:lower() end)
		for k,v in pairs(currenttable) do
			if k <= TOPSCREEN then
			else
				if y < 230 then
					if k == SELECTED then buffer:print(5, y, v, red) else buffer:print(5, y, v, white) end
					y = y + 10
				end
			end
			albums = albums + 1
		end
		MAXLIMIT = albums
	elseif ltype == 3 then
		y = 20
		songs = 0
		currenttable = {}
		for k,v in pairs(LIBRARY[LASTPARAM][param]) do
			table.insert(currenttable, k)
		end
		table.sort(currenttable, function(a, b) return a:lower() < b:lower() end)
		for k,v in pairs(currenttable) do
			if k <= TOPSCREEN then
			else
				if y < 230 then
					if k == SELECTED then buffer:print(5, y, v, red) else buffer:print(5, y, v, white) end
					y = y + 10
				end
			end
			songs = songs + 1
		end
		MAXLIMIT = songs
	end
end

while true do
	buffer:clear(black)
	buffer:blit(0, 0, background, 0, 0, buffer:width(), buffer:height(), false)
	PREVPAD = pad
	pad = Controls.read()
	if pad ~= PREVPAD then
		if pad:up() then
			if SELECTED ~= 1 then
				SELECTED = SELECTED - 1
				if SELECTED == TOPSCREEN then
					TOPSCREEN = TOPSCREEN - 1
				end
			end
		elseif pad:down() then
			if SELECTED ~= MAXLIMIT then
				SELECTED = SELECTED + 1
				if SELECTED > MAXSCREEN then
					TOPSCREEN = TOPSCREEN + 1
				end
			end
		elseif pad:right() then
			if MODE ~= 3 then
				MODE = MODE + 1
				LASTPARAM = PARAM
				PARAM = currenttable[SELECTED]
				PREVPREVSELECTED = PREVSELECTED
				PREVSELECTED = SELECTED
				SELECTED = 1
				PREVPREVTOPSCREEN = PREVTOPSCREEN
				PREVTOPSCREEN = TOPSCREEN
				TOPSCREEN = 0
			else
				nmpd:send("PLAY "..LASTPARAM.."@@@"..PARAM.."@@@"..currenttable[SELECTED].."\n")
			end
		elseif pad:left() then
			if MODE ~= 1 then
				MODE = MODE - 1
				PARAM = LASTPARAM
				SELECTED = PREVSELECTED
				PREVSELECTED = PREVPREVSELECTED
				TOPSCREEN = PREVTOPSCREEN
				PREVTOPSCREEN = PREVPREVTOPSCREEN
			end
		elseif pad:cross() then
			if MODE ~= 3 then
				MODE = MODE + 1
				LASTPARAM = PARAM
				PARAM = currenttable[SELECTED]
				PREVPREVSELECTED = PREVSELECTED
				PREVSELECTED = SELECTED
				SELECTED = 1
				PREVPREVTOPSCREEN = PREVTOPSCREEN
				PREVTOPSCREEN = TOPSCREEN
				TOPSCREEN = 0
			else
				nmpd:send("PLAY "..LASTPARAM.."@@@"..PARAM.."@@@"..currenttable[SELECTED].."\n")
			end
		elseif pad:triangle() then
			if MODE ~= 1 then
				MODE = MODE - 1
				PARAM = LASTPARAM
				SELECTED = PREVSELECTED
				PREVSELECTED = PREVPREVSELECTED
				TOPSCREEN = PREVTOPSCREEN
				PREVTOPSCREEN = PREVPREVTOPSCREEN
			end
		elseif pad:square() then
			nmpd:send("STOP\n")
		elseif pad:circle() then
			nmpd:send("TOGGLEPAUSE\n")
		elseif pad:l() then
			local volume = string.sub(nmpd:send("GETVOLUME\n"),15)
			if volume == nil or volume == "" then volume = 0 else volume = tonumber(volume) end
			if volume < 5 then
			else
				volume = volume - 5
				nmpd:send("SETVOLUME "..volume.."\n")
			end
		elseif pad:r() then
			local volume = string.sub(nmpd:send("GETVOLUME\n"),15)
			if volume == nil or volume == "" then volume = 0 else volume = tonumber(volume) end
			if volume > 95 then
			else
				volume = volume + 5
				nmpd:send("SETVOLUME "..volume.."\n")
			end
		elseif pad:start() then
			if System.Quit ~= nil then System.Quit() else break end
		elseif pad:select() then
			screen:save("screenshot.png")
		end
	end
	printListing(MODE, PARAM)
	buffer:print(200, 1, "Network Music Player Client Alpha 2", white)
	buffer:print(1, 1, "Access Point: "..apn, white)
	buffer:print(260, 240, "Memory free: "..System.getFreeMemory() / 1024 / 1024 .."Mb", white)
	buffer:print(260, 250, "Connected to server: "..tostring(nmpd:isConnected()), white)
	if PSPIP ~= nil then buffer:print(260, 260, "PSP IP: "..PSPIP, white) else buffer:print(260, 260, "PSP IP: Unknown", white) end
	screen:blit(0, 0, buffer, 0, 0, buffer:width(), buffer:height(), false)
	screen.flip()
	screen.waitVblankStart()
end

Wlan.term()
